﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private Random random = new Random();
        private int numQuestions = 20; // 题量
        private int currentQuestion = 0;
        private int correctAnswers = 0;
        private int correctAnswer;
        private bool isTestStarted = false; // 测试是否已开始的标志
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        private void Run()
        {
            if (currentQuestion < numQuestions)
            {
                int num1 = random.Next(-10, 10);
                int num2 = random.Next(-10, 10);
                int operation = random.Next(2); // 0表示减法，1表示加法
                int answer;
                if (operation == 0)
                {
                    this.textBox1.Text = $"({num1}) - ({num2})";
                    answer = num1 - num2;
                }
                else
                {
                    this.textBox1.Text = $"({num1}) + ({num2})";
                    answer = num1 + num2;
                }
                correctAnswer = answer;
                currentQuestion++;
                progressBar1.Value = 0;
                UpdateQuestionLabel();
                UpdateScoreLabel();
                StartTimer();
            }
            else
            {
                EndQuiz();
            }
        }

        private void CheckAnswer()
        {
            string userAnswerText = textBox2.Text;
            if (int.TryParse(userAnswerText, out int userAnswer))
            {
                if (userAnswer == correctAnswer)
                {
                    MessageBox.Show("回答正确!", "结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    correctAnswers++;
                    UpdateScoreLabel();
                    Run();
                }
                else
                {
                    MessageBox.Show("回答错误!", "结果", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Run();
                }
            }
            else
            {
                MessageBox.Show("请输入一个有效的数字.", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                timer1.Start();
            }
        }

        private void EndQuiz()
        {
            timer1.Stop();
            MessageBox.Show($"测试结束!\n你的得分是: {correctAnswers}/{numQuestions}", "测试结束", MessageBoxButtons.OK, MessageBoxIcon.Information);
            ResetQuiz();
        }

        private void ResetQuiz()
        {
            currentQuestion = 0;
            correctAnswers = 0;
            progressBar1.Value = 0;
            textBox2.Clear();
            textBox1.Clear();
            UpdateQuestionLabel();
            UpdateScoreLabel();
            isTestStarted = false;
        }
        private void UpdateQuestionLabel()
        {
            questionLabel.Text = $"问题数 {currentQuestion}/{numQuestions}";
        }

        private void UpdateScoreLabel()
        {
            if (currentQuestion == 0)
            {
                scoreLabel.Text = $"得分: {correctAnswers}/{currentQuestion}";

            }
            else
            {
                scoreLabel.Text = $"得分: {correctAnswers}/{currentQuestion - 1}";
            }
        }

        private void StartTimer()
        {
            timer1.Interval = 100; // 1秒
            timer1.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!isTestStarted)
            {
                ResetQuiz();
                Run();
                isTestStarted = true; // 设置测试已开始标志
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value < progressBar1.Maximum)
            {
                progressBar1.Value++;

                // 更新剩余时间的显示
                int remainingTimeInSeconds = (progressBar1.Maximum - progressBar1.Value) / 10; // 剩余时间（秒）
                labelTime.Text = $"剩余时间: {remainingTimeInSeconds} s";

                if (progressBar1.Value >= progressBar1.Maximum)
                {
                    timer1.Stop();
                    MessageBox.Show("时间到！自动跳过该题", "超时", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    Run(); // 自动跳过当前问题并出下一题
                }
            }
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (isTestStarted)
            {
                timer1.Stop();
                CheckAnswer();
            }
            else
            {
                MessageBox.Show("请先点击开始按钮。", "未开始", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }



        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }


    }
}
